<template>
	<view class="detail">
		<view class="detail-title">
			{{fromData.title}}
		</view>

		<!-- detail-header -->
		<view class="detail-header">
			<view class="detail-header_logo">
				<image :src="fromData.author.avatar" mode="aspectFill"></image>
			</view>
			<view class="detail-header_content">
				<view class="detail-header_content-title">
					{{fromData.author.author_name}}
				</view>
				<view class="detail-header_content-info">
					<text>{{fromData.create_time}}</text>
					<text>{{fromData.browse_count}} 浏览</text>
					<text>{{fromData.thumbs_up_count}} 赞</text>
				</view>
			</view>
			<button class="detail-header_button" type="default"
				@click="follow(fromData.author.id)">{{fromData.is_author_like?'已关注':'关注'}}</button>
		</view>
		<!-- content -->
		<view class="detail-content">
			<view class="detail-html">
				<u-parse :content='fromData.content' :imageProp="imageProp" :noData="noData"></u-parse>
			</view>
			<view class="detail-comment">
				<view class="comment-title">最新评论</view>
				<view class="comment-content" v-for="item in commentsList" :key="item.comment_id">
					<comments-box :comments="item" @reply="reply"></comments-box>
				</view>
			</view>
		</view>
		<!-- bottom -->
		<view class="detail-bottom">
			<view class="detail-bottom_input" @click="openComment">
				<text>在此评论</text>
				<uni-icons type="compose" size="16" color="#f07373"></uni-icons>
			</view>
			<view class="detail-bottom_icons">
				<view class="detail-bottom_icons-box" @click="openCommentList">
					<uni-icons type="chat" size="22" color="#F07373"></uni-icons>
				</view>
				<view class="detail-bottom_icons-box" @click="likeTap(fromData._id)">
					<uni-icons :type="fromData.is_like?'heart-filled':'heart'" size="22" color="#F07373"></uni-icons>
				</view>
				<view class="detail-bottom_icons-box" @click="thumbsup(fromData._id)">
					<uni-icons :type="fromData.is_thumbs_up?'hand-thumbsup-filled':'hand-thumbsup'" size="22"
						color="#F07373"></uni-icons>
				</view>
			</view>
		</view>
		<release ref="popup" @submit="submit"></release>
	</view>
</template>

<script>
	import uParse from "@/components/u-parse/u-parse.vue"
	export default {
		components: {
			uParse
		},
		data() {
			return {
				fromData: {},
				imageProp: {
					lazyLoad: true,
					mode: 'aspectFit',
					padding: 0,
					domain: ''
				},
				commentsList: [],
				replyFormData: {},
				showPopup: false,
				noData: '<p style="text-align:center;color:grey" >内容加载中...</p>'
			}
		},
		onLoad(query) {
			// console.log(JSON.parse(query.params))
			this.fromData = JSON.parse(query.params)
			this.getDetail()
			this.getComments()
		},
		methods: {
			//获取详情页真实内容
			getDetail() {
				this.$api.get_detail({
					article_id: this.fromData._id
				}).then((res) => {
					this.fromData = res.data
					console.log(this.fromData)
				})
			},
			// 更新评论
			setUpdateComment(content) {
				uni.showLoading()
				this.$api.update_comment({
					article_id: this.fromData._id,
					...content
				}).then((res) => {

					console.log(res)
					uni.hideLoading()
					uni.showToast({
						title: '评论发布成功',
						icon: 'none'
					})
					this.getComments()
					this.closeComment()
					this.replyFormData = {}
				})
			},
			//获取评论
			getComments() {
				this.$api.get_comments({
					article_id: this.fromData._id
				}).then((res) => {
					console.log(res)
					this.commentsList = res.data
				})
			},
			// 评论相关
			openComment() {
				this.$refs.popup.openComment()
			},
			closeComment() {
				this.$refs.popup.closeComment()
			},
			submit(content) {
				this.setUpdateComment({
					content,
					...this.replyFormData
				})
				console.log('发布评论')
			},
			//comments-box组件回传参数
			reply(e) {
				this.replyFormData = {
					"comment_id": e.comments.comment_id,
					"is_reply": e.is_reply
				}
				if (e.comments.reply_id) {
					this.replyFormData.reply_id = e.comments.reply_id
				}
				console.log(this.replyFormData)
				this.openComment()
			},
			//关注功能
			setUpdateAuthor(author_id) {
				uni.showLoading()
				this.$api.update_author({
					author_id
				}).then((res) => {
					console.log(res)
					uni.hideLoading()
					this.fromData.is_author_like = !this.fromData.is_author_like
					uni.$emit('update_author')
					uni.showToast({
						title: this.fromData.is_author_like ? '关注成功' : '取消关注',
						icon: 'none'
					})

				})
			},
			follow(author_id) {
				this.setUpdateAuthor(author_id)
			},
			// 收藏功能
			setUpdateLike(article_id) {
				uni.showLoading()
				this.$api.update_like({
					article_id
				}).then((res) => {
					uni.hideLoading()
					this.fromData.is_like = !this.fromData.is_like
					uni.$emit('update_article', 'follow') //通知首页更新
					uni.showToast({
						title: this.fromData.is_like ? '收藏成功' : '取消收藏',
						icon: 'none'
					})
					// console.log('收藏')
				})
			},
			likeTap(article_id) {
				this.setUpdateLike(article_id)
			},
			//点赞
			setUpdateThumbsUp(article_id) {
				uni.showLoading()
				this.$api.update_thumbsup({
					article_id
				}).then((res) => {
					console.log(res)
					uni.hideLoading()
					this.fromData.is_thumbs_up = !this.fromData.is_thumbs_up
					uni.showToast({
						title: this.fromData.is_thumbs_up ? '点赞成功' : '取消点赞',
						icon: 'none'
					})
					this.getDetail()
				})
			},
			thumbsup(article_id) {
				this.setUpdateThumbsUp(article_id)
			},
			//打开评论列表
			openCommentList() {
				uni.navigateTo({
					url: '../detail-comments/detail-comments?id=' + this.fromData._id
				})
			}
		}
	}
</script>

<style lang="scss">
	.detail {
		padding: 15px 0;
		padding-bottom: 54px;
	}

	.detail-title {
		padding: 0 15px;
		font-size: 18px;
		font-weight: 700;
		color: #333;
	}

	.detail-header {
		margin-top: 10px;
		padding: 0 15px;
		display: flex;
		align-items: center;

		.detail-header_logo {
			width: 40px;
			height: 40px;
			border-radius: 50%;
			overflow: hidden;
			flex-shrink: 0;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.detail-header_content {
			width: 100%;
			padding-left: 10px;
			display: flex;
			flex-direction: column;
			// justify-content: space-between;
			font-size: 12px;

			.detail-header_content-title {
				font-size: 14px;
				color: #333;
				margin-bottom: 5px;
			}

			.detail-header_content-info {
				color: #999;

				text {
					margin-right: 10px;
				}
			}
		}

		.detail-header_button {
			height: 30px;
			font-size: 12px;
			flex-shrink: 0;
			color: #fff;
			background-color: $mk-base-color;
		}
	}

	// content
	.detail-content {
		margin-top: 20px;
		min-height: 500px;

		.detail-html {
			padding: 0 15px;
		}

		.detail-comment {
			margin-top: 30px;

			.comment-title {
				padding: 10px 15px;
				font-size: 14px;
				color: #666;
				border-bottom: 1px #ccc solid;
			}

			.comment-content {
				padding: 0 15px;
				border-top: 1px #eee solid;
			}
		}
	}

	// bottom
	.detail-bottom {
		height: 44px;
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
		display: flex;
		align-items: center;
		border-top: 1xp #f5f5f5 solid;
		background-color: skyblue;
		box-sizing: border-box;

		.detail-bottom_input {
			height: 30px;
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-left: 10px;
			padding: 0 10px;
			border: 1px #ddd solid;
			border-radius: 6px;
		}

		.detail-bottom_icons {
			display: flex;
			flex-shrink: 0;
			padding: 0 10px;

			.detail-bottom_icons-box {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 44px;
				position: relative;
			}
		}
	}
</style>
